
#include <asm.h>

.global C_SYMBOL(A8Blit2RGB24)

C_SYMBOL(A8Blit2RGB24):
pSrc = 0x0;
srcX = 0x4;
srcY = 0x8;
width = 0xC;
height = 0x10;
srcPitch = 0x14;
srcColor = 0x18;
srcOpacity = 0x1C;

    MOV     R12, SP
    STMFD   SP!, {R4-R12,LR}

    MUL     R4, R3, R2
    ADD     R1, R4, R1, LSL#2
    ADD     R1, R0, R1

    LDR     R4, [R12,#srcPitch]
    LDR     R5, [R12,#srcY]
    LDR     R6, [R12,#srcX]
    LDR     R8, [R12,#pSrc]
    MUL     R7, R4, R5
    ADD     R6, R7, R6
    ADD     R0, R8, R6
    MOV     R2, R4

    LDR     R4, [R12,#width]
    LDR     R7, [R12,#height]
    LDR     R8, [R12,#srcColor]
    LDR     R9, [R12,#srcOpacity]


    AND     R9, R9, #0xFF
    CMP     R9, #255

    BLT     _Start_T_

	LDR LR, =0xFF000000
	ORR R8, R8, LR
	TMCRR   wR13, R8, R8
	WUNPCKELUB wR13, wR13

    MOV     R10, #0xFF000000
    TMCRR   wR12, R10, R10

    MOV R11, R0
    MOV R8,  R2
    MOV R10, R1
    MOV R12, R3

	MOV LR, R4, LSL #2

    LDR     R4, = 0x00FF00FF
    TMCRR   wR15, R4, R4

    LDR     R4, = 0x00000008
    TMCR    wCGR3, R4

//    MOV     R9, #0xFF

_LoopLine_:
	MOV	R3, LR
	MOV	R2, R10
	MOV	R0, R11
	B _Alpha_8u_C4ARow_
_Next_:
	ADD	R11, R11, R8
	ADD	R10, R10, R12
	SUBS	R7, R7,	#1
	BHI	_LoopLine_

_End_:

    LDMFD   SP, {R4-R11,SP,PC}

_Alpha_8u_C4ARow_:

    ADD	R6, R2,	#7
    BIC	R6, R6,	#7
    SUB	R6, R6,	R2
    CMP	R6, #0
    BEQ	_Aligned8_

_Process_One_:

    LDRB   R1, [R0], #1
    CMP	R6, R3
    WLDRW   wR6, [R2]

    MOVGT	R6, R3
    SUB	R3, R3,	R6

_Loop_One_:
//.long 0xe7fddefe

    SUBS	R6, R6,	#4
    TBCSTH  wR5, R1

    WOR     wR6, wR6, wR12
    WUNPCKELUB wR1, wR6
    WMULUL  wR0, wR13, wR5
    WSUBH   wR7,wR15,wR5
    WMULUL  wR1, wR1, wR7

    LDRGTB   R1, [R0], #1
    WADDH     wR0, wR0, wR1
    WSRLHG    wR0, wR0, wCGR3
    WLDRWGT   wR6, [R2, #4]

    WPACKHUS wR0, wR0, wR1

    //ADD R2,R2,#4
    WSTRW   wR0, [R2], #4

    BGT	_Loop_One_

    CMP	R3, #0
    BEQ     _Next_

_Aligned8_:
    SUBS	R3, R3,	#0x10
    ADDMI	R6, R3,	#0x10
    ADDMI	R3, R3,	#0x10
    BMI	_Process_One_
    AND	R4, R0,	#3
    CMP R4, #0
    BNE	_UnAligned8_

    WLDRW   wR0, [R0], #4
    WLDRD   wR4, [R2]

    WLDRD   wR5, [R2, #8]

_Loop_Aligned8_:

    SUBS	R3, R3,	#0x10
    PLD	[R0,#0x20]
    PLD	[R2,#0x30]

    TEXTRMUB R1, wR0, #0
    WOR     wR4, wR4, wR12
    WUNPCKELUB wR8, wR4
    TBCSTH  wR7, R1

    TEXTRMUB R1, wR0, #1

    WMULUL  wR6, wR13, wR7
    WSUBH   wR7,wR15,wR7
    WMULUL  wR8, wR8, wR7

    WUNPCKEHUB wR11, wR4
    TBCSTH   wR10, R1
    WADDH     wR6, wR6, wR8

    WSRLHG  wR6, wR6, wCGR3
    WMULUL  wR9, wR13, wR10
    WSUBH   wR10,wR15,wR10
    WMULUL  wR11, wR11, wR10

    WADDH   wR9, wR9, wR11
    WSRLHG  wR9, wR9, wCGR3

    WPACKHUS wR6, wR6, wR9
    WSTRD   wR6, [R2], #8

    TEXTRMUB R1, wR0, #2
    WOR     wR5, wR5, wR12
    WUNPCKELUB wR8, wR5
    TBCSTH  wR7, R1
    TEXTRMUB R1, wR0, #3
    WMULUL  wR6, wR13, wR7

    WSUBH   wR7,wR15,wR7
    WMULUL  wR8, wR8, wR7

    WLDRDGE wR4, [R2, #8]

    WADDH     wR6, wR6, wR8
    WSRLHG    wR6, wR6, wCGR3

    TBCSTH  wR10, R1
    WUNPCKEHUB wR11, wR5
    WMULUL  wR9, wR13, wR10

    WLDRWGE wR0, [R0], #4

    WSUBH   wR10,wR15,wR10
    WMULUL  wR11, wR11, wR10
//    WLDRDGE wR2, [R0], #8
    WADDH   wR9, wR9, wR11
    WSRLHG  wR9, wR9, wCGR3
    WPACKHUS wR6, wR6, wR9
    WLDRDGE wR5, [R2, #16]
    WSTRD wR6, [R2], #8

    BGE	_Loop_Aligned8_
    ADDS	R3, R3,	#0x10
    BEQ     _Next_
    MOVGT	R6, R3
    //SUBGT   R2, R2, #8
    BGT	_Process_One_

_UnAligned8_:
    LDRB   R1, [R0], #1
    WLDRD   wR4, [R2]

    LDRB   R5, [R0], #1
    WLDRD   wR5, [R2, #8]

_Loop_UnAligned8_:
@.long 0xe7fddefe

    SUBS	R3, R3,	#0x10
    PLD	[R0,#0x20]
    PLD	[R2,#0x30]

    TBCSTH     wR7, R1
    WOR     wR4, wR4, wR12
    WUNPCKELUB wR8, wR4

    WMULUL  wR6, wR13, wR7
    WSUBH   wR7,wR15,wR7
    WMULUL  wR8, wR8, wR7

    LDRB   R1, [R0], #1

    TBCSTH    wR10, R5
    WADDH     wR6, wR6, wR8
    WUNPCKEHUB wR11, wR4
    WSRLHG  wR6, wR6, wCGR3

    LDRB   R5, [R0], #1

    WMULUL  wR9, wR13, wR10
    WSUBH   wR10,wR15,wR10
    WMULUL  wR11, wR11, wR10

    WADDH   wR9, wR9, wR11
    WSRLHG  wR9, wR9, wCGR3

    WPACKHUS wR6, wR6, wR9


    WSTRD   wR6, [R2], #8

    TBCSTH  wR7, R1
    WOR     wR5, wR5, wR12
    WUNPCKELUB wR8, wR5
    WMULUL  wR6, wR13, wR7

    LDRGEB   R1, [R0], #1

    WSUBH   wR7,wR15,wR7
    WMULUL  wR8, wR8, wR7

    WLDRDGE wR4, [R2, #8]

    WADDH     wR6, wR6, wR8
    WSRLHG    wR6, wR6, wCGR3


    TBCSTH  wR10, R5
    WUNPCKEHUB wR11, wR5
    WMULUL  wR9, wR13, wR10

    LDRGEB   R5, [R0], #1

    WSUBH   wR10, wR15, wR10
    WMULUL  wR11, wR11, wR10

//    WLDRDGE wR2, [R0], #8

    WADDH   wR9, wR9, wR11
    WSRLHG  wR9, wR9, wCGR3


    WPACKHUS wR6, wR6, wR9
    WLDRDGE wR5, [R2, #16]

    //ADD R2,R2,#8
    WSTRD   wR6, [R2], #8

    BGE	_Loop_UnAligned8_

    ADDS	R3, R3,	#0x10
    BEQ     _Next_
    MOVGT	R6, R3
    BGT	_Process_One_

_Start_T_:
    CMP   R9, #0

    LDMEQFD   SP, {R4-R11,SP,PC}

	MOV R8, R8, LSR #8
	LDR LR, =0xFF000000
	ORR R8, R8, LR
	TMCRR   wR13, R8, R8
	WUNPCKELUB wR13, wR13

    MOV     R10, #0xFF000000
    TMCRR   wR12, R10, R10


    MOV R11, R0
    MOV R8,  R2
    MOV R10, R1
    MOV R12, R3

	MOV LR, R4, LSL #2

    LDR     R4, = 0x00FF00FF
    TMCRR   wR15, R4, R4

    LDR     R4, = 0x00000008
    TMCR    wCGR3, R4

    ORR R9, R9, R9, LSL #16
    TMCRR   wR14, R9, R9

_LoopLine_T_:
	MOV	R3, LR
	MOV	R2, R10
	MOV	R0, R11
	B _Alpha_8u_C4ARow_T_
_Next_T_:
	ADD	R11, R11, R8
	ADD	R10, R10, R12
	SUBS	R7, R7,	#1
	BHI	_LoopLine_T_

_End_T_:

	LDMFD   SP, {R4-R11,SP,PC}

_Alpha_8u_C4ARow_T_:

    ADD	R6, R2,	#7
    BIC	R6, R6,	#7
    SUB	R6, R6,	R2
    CMP	R6, #0
    BEQ	_Aligned8_T_

_Process_One_T_:

    LDRB   R1, [R0], #1
    CMP	R6, R3
    WLDRW   wR6, [R2]

    MOVGT	R6, R3
    SUB	R3, R3,	R6

_Loop_One_T_:
//.long 0xe7fddefe

    SUBS	R6, R6,	#4
    TBCSTH  wR5, R1

    WMULUL  wR5, wR14, wR5

    WOR     wR6, wR6, wR12
    WUNPCKELUB wR1, wR6
    WSRLHG    wR5, wR5, wCGR3
    WMULUL  wR0, wR13, wR5
    WSUBH   wR7,wR15,wR5
    WMULUL  wR1, wR1, wR7

    LDRGTB   R1, [R0], #1
    WADDH     wR0, wR0, wR1
    WLDRWGT   wR6, [R2, #4]
    WSRLHG    wR0, wR0, wCGR3

    WPACKHUS wR0, wR0, wR1

    //ADD R2,R2,#4
    WSTRW   wR0, [R2], #4

    BGT	_Loop_One_T_

    CMP	R3, #0
    BEQ     _Next_T_

_Aligned8_T_:
    SUBS	R3, R3,	#0x10
    ADDMI	R6, R3,	#0x10
    ADDMI	R3, R3,	#0x10
    BMI	_Process_One_T_
    AND	R4, R0,	#3
    CMP R4, #0
    BNE	_UnAligned8_T_

    WLDRW   wR0, [R0], #4
    WLDRD   wR4, [R2]

    WLDRD   wR5, [R2, #8]

_Loop_Aligned8_T_:

    SUBS	R3, R3,	#0x10
    PLD	[R0,#0x20]
    PLD	[R2,#0x30]

    TEXTRMUB R1, wR0, #0
    WOR     wR4, wR4, wR12
    WUNPCKELUB wR8, wR4
    TBCSTH  wR7, R1
    WMULUL  wR7, wR14, wR7

    TEXTRMUB R1, wR0, #1

    WSRLHG    wR7, wR7, wCGR3

    WMULUL  wR6, wR13, wR7
    WSUBH   wR7,wR15,wR7
    WMULUL  wR8, wR8, wR7

    WUNPCKEHUB wR11, wR4
    TBCSTH   wR10, R1
    WMULUL  wR10, wR14, wR10
    WADDH     wR6, wR6, wR8

    WSRLHG  wR6, wR6, wCGR3
    WSRLHG    wR10, wR10, wCGR3
    WMULUL  wR9, wR13, wR10
    WSUBH   wR10,wR15,wR10
    WMULUL  wR11, wR11, wR10

    WADDH   wR9, wR9, wR11
    WSRLHG  wR9, wR9, wCGR3

    WPACKHUS wR6, wR6, wR9
    WSTRD   wR6, [R2], #8

    TEXTRMUB R1, wR0, #2
    WOR     wR5, wR5, wR12
    WUNPCKELUB wR8, wR5
    TBCSTH  wR7, R1
    WMULUL  wR7, wR14, wR7
    TEXTRMUB R1, wR0, #3
    WSRLHG    wR7, wR7, wCGR3
    WMULUL  wR6, wR13, wR7

    WSUBH   wR7,wR15,wR7
    WMULUL  wR8, wR8, wR7

    WLDRDGE wR4, [R2, #8]

    WADDH     wR6, wR6, wR8
    WSRLHG    wR6, wR6, wCGR3

    TBCSTH  wR10, R1
    WMULUL  wR10, wR14, wR10
    WUNPCKEHUB wR11, wR5
    WSRLHG    wR10, wR10, wCGR3
    WMULUL  wR9, wR13, wR10

    WLDRWGE wR0, [R0], #4

    WSUBH   wR10,wR15,wR10
    WMULUL  wR11, wR11, wR10
//    WLDRDGE wR2, [R0], #8
    WADDH   wR9, wR9, wR11
    WSRLHG  wR9, wR9, wCGR3
    WPACKHUS wR6, wR6, wR9
    WLDRDGE wR5, [R2, #16]
    WSTRD wR6, [R2], #8
//.long 0xe7fddefe
    BGE	_Loop_Aligned8_T_
//    .long 0xe7fddefe
    ADDS	R3, R3,	#0x10
    BEQ     _Next_T_
    MOVGT	R6, R3
    //SUBGT   R2, R2, #8

    BGT	_Process_One_T_

_UnAligned8_T_:
    LDRB   R1, [R0], #1
    WLDRD   wR4, [R2]

    LDRB   R5, [R0], #1
    WLDRD   wR5, [R2, #8]

_Loop_UnAligned8_T_:
@.long 0xe7fddefe

    SUBS	R3, R3,	#0x10
    PLD	[R0,#0x20]
    PLD	[R2,#0x30]

    TBCSTH     wR7, R1
    WMULUL  wR7, wR14, wR7
    WOR     wR4, wR4, wR12
    WUNPCKELUB wR8, wR4
    WSRLHG    wR7, wR7, wCGR3

    WMULUL  wR6, wR13, wR7
    WSUBH   wR7,wR15,wR7
    WMULUL  wR8, wR8, wR7

    LDRB   R1, [R0], #1

    TBCSTH    wR10, R5
    WMULUL  wR10, wR14, wR10
    WADDH     wR6, wR6, wR8
    WUNPCKEHUB wR11, wR4
    WSRLHG  wR6, wR6, wCGR3
    WSRLHG    wR10, wR10, wCGR3

    LDRB   R5, [R0], #1

    WMULUL  wR9, wR13, wR10
    WSUBH   wR10,wR15,wR10
    WMULUL  wR11, wR11, wR10

//    WLDRDGE wR0, [R0], #8

    WADDH   wR9, wR9, wR11
    WSRLHG  wR9, wR9, wCGR3

    WPACKHUS wR6, wR6, wR9

    //ADD R2,R2,#8

    WSTRD   wR6, [R2], #8

    TBCSTH  wR7, R1
    WMULUL  wR7, wR14, wR7
    WOR     wR5, wR5, wR12
    WUNPCKELUB wR8, wR5
    WSRLHG    wR7, wR7, wCGR3
    WMULUL  wR6, wR13, wR7

    LDRGEB   R1, [R0], #1

    WSUBH   wR7,wR15,wR7
    WMULUL  wR8, wR8, wR7

    WLDRDGE wR4, [R2, #8]

    WADDH     wR6, wR6, wR8
    WSRLHG    wR6, wR6, wCGR3


    TBCSTH  wR10, R5
    WMULUL  wR10, wR14, wR10
    WUNPCKEHUB wR11, wR5
    WSRLHG    wR10, wR10, wCGR3
    WMULUL  wR9, wR13, wR10

    LDRGEB   R5, [R0], #1

    WSUBH   wR10, wR15, wR10
    WMULUL  wR11, wR11, wR10

//    WLDRDGE wR2, [R0], #8

    WADDH   wR9, wR9, wR11
    WSRLHG  wR9, wR9, wCGR3


    WPACKHUS wR6, wR6, wR9
    WLDRDGE wR5, [R2, #16]

    //ADD R2,R2,#8
    WSTRD   wR6, [R2], #8

    BGE	_Loop_UnAligned8_T_

    ADDS	R3, R3,	#0x10
    BEQ     _Next_T_
    MOVGT	R6, R3
//    .long 0xe7fddefe
    BGT	_Process_One_T_
